<template>
  <!-- Excel 导入功能组件 -->
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <UploadExcle :on-success="handleSuccess" />
      </el-card>
    </div>
  </div>
</template>

<script>
import { formatExcelDate } from '@/utils'
import { importEmployees } from '@/api/employees'
export default {
  name: 'Excel',
  methods: {
    transExcel(results) {
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      return results.map(item => {
        const obj = {}
        const contentKeys = Object.keys(item)
        contentKeys.forEach(key => {
          const transKey = userRelations[key]
          if (transKey === 'timeOfEntry' || transKey === 'correctionTime') {
            obj[transKey] = new Date(formatExcelDate(item[key]))
          } else {
            obj[transKey] = item[key]
          }
        })
        return obj
      })
    },
    // 导入成功以后的回调函数
    async handleSuccess({ results, header }) {
      const arr = this.transExcel(results)
      const res = await importEmployees(arr)
      console.log(res)
      this.$router.back()
      this.$message.success(res.message)
    }
  }
}
</script>

<style lang="scss" scoped></style>

